package com.gy.assistant.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMGroupMessageReceiptMemberListQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.entity.ZIMGroupMessageReceiptMemberQueryConfig;
import im.zego.zim.entity.ZIMMessage;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.enums.MessageLoadType;
import com.gy.assistant.ui.BaseActivity;
import com.gy.assistant.ui.chat.group.adapter.GroupMemberListAdapter;

public class ZIMReceiptGroupMemberActivity extends BaseActivity implements ZIMGroupMessageReceiptMemberListQueriedCallback {

    private static final String TAG = "ZIMGroupMemberSelectActivity";
    private String groupID;
    private boolean isSelectRead = false;
    private int QUERY_MEMBER_COUNT = 30;
    private int queryNextFlag = 0;
    private MessageLoadType messageLoadType;
    public static final int SELECT_MEMBER_RESULT_CODE = 200;
    private static ZIMMessage sMessage;

    private GroupMemberListAdapter memberListAdapter = new GroupMemberListAdapter();

    public static void actionStart(Activity activity, String groupID,boolean selectRead,ZIMMessage message) {
        Intent intent = new Intent(activity, ZIMReceiptGroupMemberActivity.class);
        intent.putExtra("groupID", groupID);
        intent.putExtra("selectRead", selectRead);
        sMessage = message;
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt_group_member);
        groupID = getIntent().getStringExtra("groupID");
        isSelectRead = getIntent().getBooleanExtra("selectRead",false);

        RecyclerView recyclerView = findViewById(R.id.rv_member);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(memberListAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == memberListAdapter.getItemCount()) {
                    if (messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_LOADING && messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA) {
                        queryMemberList();
                    }
                }
            }
        });
        memberListAdapter.showCheckBox = false;
        queryMemberList();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void queryMemberList() {
        messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOADING;
        ZIMGroupMessageReceiptMemberQueryConfig config = new ZIMGroupMessageReceiptMemberQueryConfig();
        config.count = QUERY_MEMBER_COUNT;
        config.nextFlag = queryNextFlag;
        if (isSelectRead){
            SDKManager.sharedInstance().queryGroupMessageReceiptReadMemberList(sMessage,groupID,config,this);
        }else {
            SDKManager.sharedInstance().queryGroupMessageReceiptUnreadMemberList(sMessage,groupID,config,this);
        }
    }

    @Override
    public void onGroupMessageReceiptMemberListQueried(String groupID, ArrayList<ZIMGroupMemberInfo> userList, int nextFlag, ZIMError errorInfo) {
        if (errorInfo.code== ZIMErrorCode.SUCCESS){
            memberListAdapter.addAll(userList);
            queryNextFlag = nextFlag;
            if (nextFlag == 0) {
                messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA;
            } else {
                messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOAD_COMPLETE;
            }
        }
    }
}
